package sw.EngAdroid;


import java.util.ArrayList;
import java.util.List;

import netcode.client.Client;
import netcode.helper.ClientMessageListener;
import netcode.helper.NetcodeHelper;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.util.DisplayMetrics;
import android.view.Window;

public class ThreePlayerActivity extends Activity{

	private TextView currentName;
	private TextView questionText;
	private TextView question;
	private EditText answerText;
	private ArrayList<String> playerNames;
	private Button start;
	private Button submit;
	private boolean notdone;
	private int currentPlayer;
	private Game game;
	private int numberOfPlayers;
	private TextView myCounter;
	private sw.EngAdroid.CountDownTimer t;
	private Button bet0;
	private TextView guess0;
	private TextView ratio0;
	private EditText chips0;
	private Button bet1;
	private TextView guess1;
	private TextView ratio1;
	private EditText chips1;
	private Button bet2;
	private TextView guess2;
	private TextView ratio2;
	private EditText chips2;
	private TextView p0chips;
	private TextView p1chips;
	private TextView p2chips;
	private TextView correctGuess;
	private Button nextRound;
	private Button endGame;
	private TextView playerNameBet;
	private boolean isLocal = true;
	private boolean small = false;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		DisplayMetrics dm = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(dm);
		Intent intent = getIntent();
		Bundle info = intent.getExtras();

			if (dm.widthPixels < 1000) {
				setContentView(R.layout.question_screen2);
				small = true;
			} else
				setContentView(R.layout.question_screen);
			startGame();

	}

	@Override
	protected void onStart() {
		super.onStart();
	}

	@Override
	protected void onResume() {
		super.onResume();
	}

	@Override
	protected void onPause() {
		super.onPause();
	}

	@Override
	protected void onStop() {
		super.onStop();
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
	}

	private void startGame() {
		currentPlayer = 0;
		notdone = true;
		Intent intentInfo = getIntent();
		Bundle info = intentInfo.getExtras();
		numberOfPlayers = playerNames.size();
		if (isLocal == true)
			game = new Game("Test", playerNames.size());
		else
			game = new NetworkGame(info.getString("gameName"),
					playerNames.size());
		for (int i = 0; i < playerNames.size(); i++) {
			Player p = new Player(playerNames.get(i));
			p.joinGame(game);
		}

		placeGuess();
	}

	// Place guess in local game
	private void placeGuess() {
		if (small)
			setContentView(R.layout.question_screen2);
		else
			setContentView(R.layout.question_screen);

		currentName = (TextView) findViewById(R.id.playerName);
		questionText = (TextView) findViewById(R.id.questionText);
		answerText = (EditText) findViewById(R.id.answerTextBox);
		submit = (Button) findViewById(R.id.submitAnswerButton);
		currentName.setText(((Player) (game.getPlayers().get(currentPlayer)))
				.getPlayer_name());
		questionText.setText(game.getQuestion().getText());
		myCounter = (TextView) findViewById(R.id.timer);

		t = new sw.EngAdroid.CountDownTimer(30000, 1000) {
			@Override
			public void onTick(long millisUntilFinished) {
				myCounter.setText("Time Remaining: " + millisUntilFinished
						/ 1000);

			}

			@Override
			public void onFinish() {
				myCounter.setText("Times up!");
				answerText.setText("000000");
				getAnswer();
				submit.performClick();
			}
		}.start();

		getAnswer();

	}

	private void placeBets() {
		if (small)
			setContentView(R.layout.playerboard3small);
		else
			setContentView(R.layout.playerboard3);

		bet0 = (Button) findViewById(R.id.confirm01);
		question = (TextView) findViewById(R.id.questionTextView1);
		guess0 = (TextView) findViewById(R.id.guess01);
		ratio0 = (TextView) findViewById(R.id.ratio01);
		chips0 = (EditText) findViewById(R.id.chips01);
		// bet1 = (Button)findViewById(R.id.confirm02);
		guess1 = (TextView) findViewById(R.id.guess02);
		ratio1 = (TextView) findViewById(R.id.ratio02);
		chips1 = (EditText) findViewById(R.id.chips02);
		// bet2 = (Button)findViewById(R.id.confirm03);
		guess2 = (TextView) findViewById(R.id.guess03);
		ratio2 = (TextView) findViewById(R.id.ratio03);
		chips2 = (EditText) findViewById(R.id.chips03);
		playerNameBet = (TextView) findViewById(R.id.gameNameView);
		playerNameBet.setText("Player name: "
				+ ((Player) game.getPlayers().get(currentPlayer))
						.getPlayer_name());

		List<Guess> guesses = game.getGuesses();
		question.setText(game.getQuestion().getText());
		ratio0.setText("Ratio: 2.0");
		ratio1.setText("Ratio: 2.0");
		ratio2.setText("Ratio: 2.0");
		guess0.setText("Guess: " + guesses.get(0).getGuess());
		guess1.setText("Guess: " + guesses.get(1).getGuess());
		guess2.setText("Guess: " + guesses.get(2).getGuess());

		myCounter = (TextView) findViewById(R.id.Timer);

		t = new sw.EngAdroid.CountDownTimer(30000, 1000) {
			@Override
			public void onTick(long millisUntilFinished) {
				myCounter.setText("Time Remaining: " + millisUntilFinished
						/ 1000);

			}

			@Override
			public void onFinish() {
				myCounter.setText("Times up!");
				bet0.performClick();
			}
		}.start();

		bet0.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View v) {
				t.cancel();
				Player player = ((Player) game.getPlayers().get(currentPlayer));
				String one = chips0.getText().toString();
				if (!(one.equals("")))
					player.placeBet(0, Integer.parseInt(one));
				String two = chips1.getText().toString();
				if (!(two.equals("")))
					player.placeBet(1, Integer.parseInt(two));
				String three = chips2.getText().toString();
				if (!(three.equals("")))
					player.placeBet(2, Integer.parseInt(three));
				if (currentPlayer < (game.getPlayers().size() - 1)) {
					currentPlayer++;
					placeBets();
				} else {
					currentPlayer = 0;
					game.resetChipBets();
					winnings();
				}
			}
		});
		// bet1.setOnClickListener(new Button.OnClickListener() {
		// public void onClick(View v){
		// Player player = ((Player)game.getPlayers().get(currentPlayer));
		// player.placeBet(1, Integer.parseInt(chips1.getText().toString()));
		// if(currentPlayer<(game.players.size()-1)){
		// currentPlayer++;
		// placeBets();
		// }
		// else{
		// currentPlayer=0;
		// winnings();
		// }
		// }
		// });
		// bet2.setOnClickListener(new Button.OnClickListener() {
		// public void onClick(View v){
		// Player player = ((Player)game.getPlayers().get(currentPlayer));
		// player.placeBet(2, Integer.parseInt(chips2.getText().toString()));
		// if(currentPlayer<(game.players.size()-1)){
		// currentPlayer++;
		// placeBets();
		// }
		// else{
		// currentPlayer=0;
		// winnings();
		// }
		// }
		// });
	}

	private void winnings() {
		if (small)
			setContentView(R.layout.winnings3small);
		else
			setContentView(R.layout.winnings3);

		game.winnings();
		p0chips = (TextView) findViewById(R.id.player0);
		p1chips = (TextView) findViewById(R.id.player1);
		p2chips = (TextView) findViewById(R.id.player2);
		correctGuess = (TextView) findViewById(R.id.correctGuess);
		correctGuess.setText("Correct answer: "
				+ game.getQuestion().getAnswer());
		p0chips.setText(game.getPlayers().get(0).getPlayer_name() + "'s chips: "
				+ game.getPlayers().get(0).getChips());
		p1chips.setText(game.getPlayers().get(1).getPlayer_name() + "'s chips: "
				+ game.getPlayers().get(1).getChips());
		p2chips.setText(game.getPlayers().get(2).getPlayer_name() + "'s chips: "
				+ game.getPlayers().get(2).getChips());
		nextRound = (Button) findViewById(R.id.again);
		endGame = (Button) findViewById(R.id.endGame);
		game.displayAnswer();
		//game.clearBets();
		//game.clearGuesses();

		nextRound.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View v) {
				//int temp = game.nextQuestion;
				//temp++;
				//game.nextQuestion = temp;
				placeGuess();
			}
		});

		endGame.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View v) {
				finish();
			}
		});

	}

	private void getAnswer() {
		submit.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View v) {
				Player player = ((Player) game.getPlayers().get(currentPlayer));
				int numGuess = Integer
						.parseInt(answerText.getText().toString());
				// Guess guess = new Guess(player.getPlayer_name(), numGuess);
				player.submitGuess(numGuess);
				if (currentPlayer < (game.getPlayers().size() - 1)) {
					currentPlayer++;
					t.cancel();
					placeGuess();
				} else {
					t.cancel();
					currentPlayer = 0;
					placeBets();
				}
			}
		});
	}

}
